package cn.edu.uestc.smgt.controller;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.uestc.smgt.pojo.Manager;
import cn.edu.uestc.smgt.pojo.Unit;
import cn.edu.uestc.smgt.pojo.User;
import cn.edu.uestc.smgt.service.ManagerService;
import cn.edu.uestc.smgt.service.UnitService;
import cn.edu.uestc.smgt.service.UserService;

@Controller
@RequestMapping("/login/")
public class LoginController {
	@Autowired
	private ManagerService managerService;
	@Autowired
	private UserService userService;
	@Autowired
	private UnitService unitService;

	/**
	 * 管理员登录
	 * 
	 * @param map
	 * @param session
	 * @return
	 */
	@RequestMapping("mgr")
	@ResponseBody
	public HashMap login(@RequestParam HashMap map, HttpSession session) {
		String username = (String) map.get("username");
		String password = (String) map.get("password");
		HashMap result = new HashMap();
		String message = null;
		int status = 0;
		if (username == null || "".equals(username)) {
			message = "账号不能为空";
			status = 0;
		} else {
			Manager manager = managerService.managerLogin(username, password);
			if (manager == null) {
				message = "账号或密码错误";
				status = 0;
			} else {
				session.setAttribute("manager", manager);
				message = "登录成功";
				status = 1;
			}
		}
		result.put("message", message);
		result.put("status", status);
		return result;
	}

	/**
	 * 管理员登录状态判断
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("mgr/loginstatus")
	@ResponseBody
	public HashMap getManagerLoginStatu(HttpSession session) {
		HashMap result = new HashMap();
		Object mgr = session.getAttribute("manager");
		if (mgr == null) {
			result.put("status", 0);
			result.put("message", "请登录");
		} else {
			result.put("status", 1);
			result.put("message", "已登录");
		}
		return result;
	}

	/**
	 * 获取管理员信息。
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("mgr/mgrinfo")
	@ResponseBody
	public HashMap getManagerInfo(HttpSession session) {
		HashMap result = new HashMap();
		Object mgr = session.getAttribute("manager");
		if (mgr == null) {
			result.put("status", 0);
			result.put("message", "请登录");
		} else {
			result.put("status", 1);
			result.put("username", ((Manager) mgr).getUsername());
			result.put("message", "已登录");
		}
		return result;
	}

	/**
	 * 修改管理员密码
	 */
	@RequestMapping("mgr/changepwd")
	@ResponseBody
	public HashMap changeMgrPwd(@RequestParam HashMap map, HttpSession session) {
		HashMap result = new HashMap();
		Object mgr = session.getAttribute("manager");
		if (mgr == null) {
			result.put("status", 0);
			result.put("message", "请登录");
		} else {
			String username = ((Manager) mgr).getUsername();
			String password = (String) map.get("password");
			String newPwd = (String) map.get("newPwd");
			if (managerService.managerChangePwd(username, password, newPwd)) {
				result.put("status", 1);
				result.put("message", "修改成功");
			} else {
				result.put("status", 0);
				result.put("message", "修改失败");
			}
		}
		return result;
	}

	/**
	 * 二级用户登录
	 * 
	 * @param map
	 * @param session
	 * @return
	 */
	@RequestMapping("yh")
	@ResponseBody
	public HashMap yhLogin(@RequestParam HashMap map, HttpSession session) {
		String username = (String) map.get("username");
		String password = (String) map.get("password");
		HashMap result = new HashMap();
		String message = null;
		int status = 0;
		if (username == null || "".equals(username)) {
			message = "账号不能为空";
			status = 0;
		} else {
			User user = userService.yhLogin(username, password);
			if (user == null) {
				message = "账号或密码错误";
				status = 0;
			} else {
				session.setAttribute("user", user);
				message = "登录成功";
				status = 1;
			}
		}
		result.put("message", message);
		result.put("status", status);
		return result;
	}

	/**
	 * 获取二级用户信息。
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("yh/yhinfo")
	@ResponseBody
	public HashMap getYhInfo(HttpSession session) {
		HashMap result = new HashMap();
		User yh = (User) session.getAttribute("user");
		if (yh == null) {
			result.put("status", 0);
			result.put("message", "请登录");
		} else {
			String unitId = yh.getUnitId();
			// 查询对应的单位名称
			Unit u = unitService.getUnitById(unitId);
			result.put("status", 1);
			result.put("username", ((User) yh).getUsername());
			result.put("unitName", u.getName());
			result.put("message", "已登录");
		}
		return result;
	}

	/**
	 * 修改二级用户密码
	 */
	@RequestMapping("yh/changepwd")
	@ResponseBody
	public HashMap changeYhPwd(@RequestParam HashMap map, HttpSession session) {
		HashMap result = new HashMap();
		Object yh = session.getAttribute("user");
		if (yh == null) {
			result.put("status", 0);
			result.put("message", "请登录");
		} else {
			String username = ((User) yh).getUsername();
			String password = (String) map.get("password");
			String newPwd = (String) map.get("newPwd");
			if (userService.yhChangePwd(username, password, newPwd)) {
				result.put("status", 1);
				result.put("message", "修改成功");
			} else {
				result.put("status", 0);
				result.put("message", "修改失败");
			}
		}
		return result;
	}

}
